package com.ys.util.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.bo.util.StringUtil;
import com.ys.dto.RowClickGridConfigDto;
import com.ys.entity.sys.YsDict;
import com.ys.entity.sys.YsRowClickGrid;
import com.ys.entity.sys.YsRowClickGridCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.enume.sys.YsGridColEnum;

public class RowClickGridUtil {
	
	/*
	 * 获取bootstrap table的columns
	 */
	public static RowClickGridConfigDto getRowClickGridConfigDto(YsRowClickGrid ysRowClickGrid, List<YsRowClickGridCol> listYsRowClickGridCol
			) {
		RowClickGridConfigDto dto = null;
		List<RowClickGridConfigDto.ColDto> colArr = new ArrayList<RowClickGridConfigDto.ColDto>();
		
		// 选中框
		{
			Long id = null;
			String title = null;
			String field = null;
			String type = "checkbox";
			boolean hide = false;
			String width = "50";
			String align = null;
			String modalRelativeCode = null;
			String modalValueCode = null;
			String dictKeyBase = null;
			String modalKey = null;
			String templet = null;
			Integer stateEdit = null;
			Integer multyFileSourceType = null;
			boolean unresize = true;

			colArr.add(new RowClickGridConfigDto.ColDto(id, title, field, type, hide, width, align, modalRelativeCode, modalValueCode, dictKeyBase, modalKey, templet, stateEdit, multyFileSourceType,unresize));
		}


		// 序号列
		{
			Long id = null;
			String title = "序号";
			String field = null;
			String type = "numbers";
			boolean hide = false;
			String width = "50";
			String align = null;
			String modalRelativeCode = null;
			String modalValueCode = null;
			String dictKeyBase = null;
			String modalKey = null;
			String templet = null;
			Integer stateEdit = null;
			Integer multyFileSourceType = null;
			boolean unresize = true;

			colArr.add(new RowClickGridConfigDto.ColDto(id, title, field, type, hide, width, align, modalRelativeCode, modalValueCode, dictKeyBase, modalKey, templet, stateEdit, multyFileSourceType,unresize));
		}
		
		
		for (YsRowClickGridCol ysRowClickGridCol : listYsRowClickGridCol) {

			Long id = ysRowClickGridCol.getId();
			String title = ysRowClickGridCol.getName();
			String field = StringUtil.firstCharacterToLower(ysRowClickGridCol.getCode());
			String type = "normal";//ysGridCol.getFrontType();
			boolean hide = ysRowClickGridCol.getFrontShow() == YsGridColEnum.FrontShow.SHOW.value ? false : true;
			String width = "" + ysRowClickGridCol.getFrontWidth();
			String align = "center";
			String modalRelativeCode = ysRowClickGridCol.getModalRelativeCode();
			String modalValueCode = ysRowClickGridCol.getModalValueCode();
			String dictKeyBase = ysRowClickGridCol.getDictKeyBase();
			String modalKey = ysRowClickGridCol.getModalKey();
			String templet = null;
			Integer stateEdit = ysRowClickGridCol.getFrontEdit();
			Integer multyFileSourceType = ysRowClickGridCol.getMultyFileSourceType();
			boolean unresize = false;
			
			if (ysRowClickGridCol.getFrontEdit() == YsGridColEnum.FrontEdit.YES.value) {

				if (YsGridColEnum.FrontType.TEXT.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.TEXT_NUMBER.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.DATE_YYMM.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.DATE_YYMMDD.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.SELECTOR_MODAL_SINGLE.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.IMG.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.FILE.value.equals(ysRowClickGridCol.getFrontType())
						|| YsGridColEnum.FrontType.FILE_MULTY.value.equals(ysRowClickGridCol.getFrontType())) {
					templet = "#" + getTempletId(ysRowClickGridCol);
				}
			} else {

				if (YsColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysRowClickGridCol.getFrontType())) {

					if(ysRowClickGridCol.getDictKeyBase() == null || ysRowClickGridCol.getDictKeyBase().length() == 0) {
						templet = "function(d){layer.msg('" + ysRowClickGridCol.getCode() + "尚未设置字典')}";
					} else {
						templet = "function(d){ var options = MAP_DICT['" + ysRowClickGridCol.getDictKeyBase() + "']; "
								+ "for(var i=0;i<options.length;i++){"
								+ "var option = options[i];"
								+ "if(option.value == d." + ysRowClickGridCol.getCode() + "){"
								+ "return option.name;"
								+ "}"
								+ "}"
								+ "return ``;"
								+ "}";
					}
				} else if (YsColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysRowClickGridCol.getFrontType())) {

					if(ysRowClickGridCol.getDictKeyBase() == null || ysRowClickGridCol.getDictKeyBase().length() == 0) {
						templet = "function(d){layer.msg('" + ysRowClickGridCol.getCode() + "尚未设置字典')}";
					} else {
						templet = "function(d){ var options = MAP_DICT['" + ysRowClickGridCol.getDictKeyBase() + "']; "
								+ "for(var i=0;i<options.length;i++){"
								+ "var option = options[i];"
								+ "if(option.value == d." + ysRowClickGridCol.getCode() + "){"
								+ "return option.name;"
								+ "}"
								+ "}"
								+ "return ``;"
								+ "}";
					}
				} else if (YsGridColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysRowClickGridCol.getFrontType())) {
					templet = "#" + getTempletId(ysRowClickGridCol);
				}else if (YsColEnum.FrontType.IMG.value.equals(ysRowClickGridCol.getFrontType())) {//图片
					templet = "function(d){"
							+ "if(!d." + ysRowClickGridCol.getCode() + ") {return ``;}"
							+ "return `<img src=\"` + d." + ysRowClickGridCol.getCode() + " + `\" style=\"max-height:30px; max-width: 30px;\"  onclick=\"layer.photos({photos: {'title': '图片预览','start': 0,'data': [{'src': '` +  d." + ysRowClickGridCol.getCode() + " + ` '}]},closeBtn: 1});\" />`;"
							+ "}";
				} else if (YsColEnum.FrontType.FILE_MULTY.value.equals(ysRowClickGridCol.getFrontType())) {//附件（多）


					templet = "function(d){"
							+ "if(!d." + ysRowClickGridCol.getCode() + ") {return ``;}"
							+"return `<input id=\"id_multy_file_" + System.currentTimeMillis() + "\" class=\"layui-input\" style=\"text-align: "+YsColEnum.FrontAlign.getLayerAlign(ysRowClickGridCol.getFrontAlign())+";cursor: pointer;color: #4DB6FD;\" code=\"" + ysRowClickGridCol.getCode() + "\"  data-filter=\"_input_filter_file_multy_on_table_in_form\" data-source-code=\"" + ysRowClickGridCol.getCode() + "\" data-source-type=\"" + ysRowClickGridCol.getMultyFileSourceType() + "\" data-source-id=\"`+d.sourceId+`\" value=\"`+d." + ysRowClickGridCol.getCode() + "+`\" readonly=\"readonly\" />`;"
							+ "}";

				} else if (YsColEnum.FrontType.FILE.value.equals(ysRowClickGridCol.getFrontType())) {//附件  layui-icon-download-circle

					templet = "function(d){"
							+ "if(!d." + ysRowClickGridCol.getCode() + ") {return ``;}"
							+"return `<i title=\"点击下载\" class=\"layui-icon layui-icon-file\" style=\"text-align: "+YsColEnum.FrontAlign.getLayerAlign(ysRowClickGridCol.getFrontAlign())+";cursor: pointer;color: #4DB6FD;font-size: 20px;font-weight:bold;\" data-url=\"`+d." + ysRowClickGridCol.getCode() + "+`\" data-filter=\"_input_filter_file_on_table_in_form\"  ></i> `;"
							+ "}";
				} else if (YsColEnum.FrontType.HYPER_LINK.value.equals(ysRowClickGridCol.getFrontType())) {//超链接

					templet = "function(d){"
							+ "if(!d." + ysRowClickGridCol.getCode() + ") {return ``;}"
							+"return `<a style=\"text-decoration: underline;cursor: pointer;color: #4DB6FD;\" onclick=\"window.open(\'`+d." + ysRowClickGridCol.getCode() + "+`\');\" >`+d." + ysRowClickGridCol.getCode() + "+`</a> `;"
							+ "}";
				}
			}
			colArr.add(new RowClickGridConfigDto.ColDto(id, title, field, type, hide, width, align, modalRelativeCode, modalValueCode, dictKeyBase, modalKey, templet, stateEdit, multyFileSourceType,unresize));
		}
		
		Long rowClickGridId = ysRowClickGrid.getId();
		String pathRequest = ysRowClickGrid.getPathRequest();
		dto = new RowClickGridConfigDto(rowClickGridId, pathRequest,GridUtil.RowClickGridConfigDtoToString(colArr));
		return dto;
	}
	
	public static List<RowClickGridConfigDto.TempletDto> listTemplets(List<YsRowClickGridCol> listYsRowClickGridCol,
			Map<String, List<YsDict>> KEY_LISTDICT_MAP) {
		List<RowClickGridConfigDto.TempletDto> list = new ArrayList<RowClickGridConfigDto.TempletDto>();
		for (YsRowClickGridCol ysRowClickGridCol : listYsRowClickGridCol) {
			if (ysRowClickGridCol.getFrontEdit() == YsGridColEnum.FrontEdit.YES.value) {
				if (YsGridColEnum.FrontType.TEXT.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysRowClickGridCol.getCode() + "\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_text_on_sub_grid\">";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.TEXT_NUMBER.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "<input type=\"number\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysRowClickGridCol.getCode() + "\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_text_on_sub_grid\">";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysRowClickGridCol.getFrontType())) {
					
					if(ysRowClickGridCol.getDictKeyBase() == null || ysRowClickGridCol.getDictKeyBase().length() == 0) {
						String id = getTempletId(ysRowClickGridCol);
						String html = "{{#  layer.msg('" + ysRowClickGridCol.getCode() + "尚未设置字典'); }}";
						list.add(new RowClickGridConfigDto.TempletDto(id, html));
					} else {
						String id = getTempletId(ysRowClickGridCol);
						String html = 
								"<select name=\"" + ysRowClickGridCol.getCode() + "\" lay-filter=\"_input_filter_selector_on_table_in_form\">"
								+ "{{#  layui.each(" + JSON.toJSONString(KEY_LISTDICT_MAP.get(ysRowClickGridCol.getDictKeyBase())) + ", function(index, item){ }}"
								+ "{{# if(d." + ysRowClickGridCol.getCode() + " == item.dictValue){ }} <option value=\"{{ item.dictValue }}\" selected>{{ item.dictName }}</option> {{#  } else { }} <option value=\"{{ item.dictValue }}\">{{ item.dictName }}</option>  {{#  } }} ";
						html += "{{#  }); }}";
						html += "</select>";
						list.add(new RowClickGridConfigDto.TempletDto(id, html));
					}
				} else if (YsGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysRowClickGridCol.getFrontType())) {
					
					if(ysRowClickGridCol.getDictKeyBase() == null || ysRowClickGridCol.getDictKeyBase().length() == 0) {
						String id = getTempletId(ysRowClickGridCol);
						String html = "{{#  layer.msg('" + ysRowClickGridCol.getCode() + "尚未设置字典'); }}";
						list.add(new RowClickGridConfigDto.TempletDto(id, html));
					} else {
						
						String layText = "";
						for (YsDict ysDict : KEY_LISTDICT_MAP.get(ysRowClickGridCol.getDictKeyBase())) {
							layText += ysDict.getDictName() + "|";
						}
						
						String id = getTempletId(ysRowClickGridCol);
						String html = "<input type=\"checkbox\" name=\"" + ysRowClickGridCol.getCode() + "\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" lay-skin=\"switch\" lay-text=\"" + layText + "\" lay-filter=\"_input_filter_checkbox_on_sub_grid\" {{ d." + ysRowClickGridCol.getCode() + " == '1' ? 'checked' : '' }}>";
						list.add(new RowClickGridConfigDto.TempletDto(id, html));
					}
				} else if (YsGridColEnum.FrontType.SELECTOR_MODAL_SINGLE.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysRowClickGridCol.getCode() + "\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" data-modal-key=\"" + ysRowClickGridCol.getModalKey() + "\" data-filter=\"_input_filter_modal_single_on_sub_grid\" readonly>";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.DATE_YYMM.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysRowClickGridCol.getCode() + "\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_date_yyyymm_on_sub_grid\">";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.DATE_YYMMDD.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysRowClickGridCol.getCode() + "\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_date_day_on_sub_grid\">";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.IMG.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "{{# if(d." + ysRowClickGridCol.getCode() + "){ }} <img code=\"" + ysRowClickGridCol.getCode() + "\" src=\"{{d." + ysRowClickGridCol.getCode() + "}}\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_img_on_table_in_form\"/><i code=\"" + ysRowClickGridCol.getCode() + "\" class=\"layui-icon\" style=\"color: red;\" title=\"移除\" data-filter=\"_icon_remove_filter_img_on_table_in_form\">&#x1007;</i>{{#  } else { }}  <img code=\"" + ysRowClickGridCol.getCode() + "\" src=\"https://admin.mini-ease.com/static/img/img_add.jpg\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_img_on_table_in_form\"/>   {{#  } }}";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.FILE.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "{{# if(d." + ysRowClickGridCol.getCode() + "){ }} <img code=\"" + ysRowClickGridCol.getCode() + "\" src=\"https://admin.mini-ease.com/static/img/img_file.jpg\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_file_single_on_table_in_form\"/><i code=\"" + ysRowClickGridCol.getCode() + "\" class=\"layui-icon\" style=\"color: red;\" title=\"移除\" data-filter=\"_icon_remove_filter_file_single_on_table_in_form\">&#x1007;</i><i code=\"" + ysRowClickGridCol.getCode() + "\" class=\"layui-icon\" style=\"color: blue;\" title=\"下载\" data-filter=\"_icon_download_filter_file_single_on_table_in_form\">&#xe615;</i>{{#  } else { }}  <img code=\"" + ysRowClickGridCol.getCode() + "\" src=\"https://admin.mini-ease.com/static/img/img_add.jpg\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_file_single_on_table_in_form\"/>   {{#  } }}";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.FILE_MULTY.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
//					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_text_on_sub_grid\">";
					String html = "{{# if(d.id){ }} <input id=\"id_multy_file_" + System.currentTimeMillis() + "\" class=\"layui-input\" code=\"" + ysRowClickGridCol.getCode() + "\"  data-filter=\"_input_filter_file_multy_on_table_in_form\" data-source-code=\"" + ysRowClickGridCol.getCode() + "\" data-source-type=\"" + ysRowClickGridCol.getMultyFileSourceType() + "\" data-source-id=\"{{d.id}}\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" readonly=\"readonly\" />{{#  } else { }}  <span>保存后可上传</span>   {{#  } }}";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = ysRowClickGridCol.getTplScript();
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				}
			} else {

				if (YsGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = 
							"{{#  layui.each(" + JSON.toJSONString(KEY_LISTDICT_MAP.get(ysRowClickGridCol.getDictKeyBase())) + ", function(index, item){ }}"
							+ "{{# if(d." + ysRowClickGridCol.getCode() + " == item.dictValue){ }} {{ item.dictName }} {{#  } }} ";
					html += "{{#  }); }}";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysRowClickGridCol.getFrontType())) {
					
					String layText = "";
					for (YsDict ysDict : KEY_LISTDICT_MAP.get(ysRowClickGridCol.getDictKeyBase())) {
						layText += ysDict.getDictName() + "|";
					}
					
					String id = getTempletId(ysRowClickGridCol);
					String html = "<input type=\"checkbox\" name=\"" + ysRowClickGridCol.getCode() + "\" value=\"{{d." + ysRowClickGridCol.getCode() + "}}\" lay-skin=\"switch\" lay-text=\"" + layText + "\" lay-filter=\"_input_filter_checkbox_on_table_in_form\" {{# if(d." + ysRowClickGridCol.getCode() + " === '1'){ }} checked {{#  } }} disabled>";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.IMG.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = "{{# if(d." + ysRowClickGridCol.getCode() + "){ }} <img src=\"{{d." + ysRowClickGridCol.getCode() + "}}\" style=\"max-height:30px; max-width: 30px;\"/>{{#  } }}";
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysRowClickGridCol.getFrontType())) {
					String id = getTempletId(ysRowClickGridCol);
					String html = ysRowClickGridCol.getTplScript();
					list.add(new RowClickGridConfigDto.TempletDto(id, html));
				}
//				else if (YsColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysGridCol.getFrontType())) {
//					templet = "#" + getTempletId(ysGridCol);
//				}
			}
		}
		
		return list;
	}
	
	/*
	 * 获取templet的命名
	 * 格式：templet_script_xxxx
	 */
	public static String getTempletId(YsRowClickGridCol ysRowClickGridCol) {
		
		return "templet_script_row_click_grid_" + ysRowClickGridCol.getId();
	}
	
	/*
	 * 获取表单lay-filter的命名
	 * 格式：input_filter_xxxx
	 */
	public static String getFilterNameInput(YsRowClickGridCol ysRowClickGridCol) {
		
		return "row_click_grid_input_filter_" + ysRowClickGridCol.getCode();
	}
	
	/*
	 * 获取选择框lay-filter的命名
	 * 格式：select_filter_xxxx
	 */
	public static String getFilterNameSelector(YsRowClickGridCol ysRowClickGridCol) {
		
		return "row_click_grid_select_filter_" + ysRowClickGridCol.getCode();
	}
	
	public static String ColGridToString(List<RowClickGridConfigDto.ColDto> colArr) {
		StringBuffer sb = new StringBuffer("[[");
		for (int i = 0; colArr != null && i < colArr.size(); i++) {
			RowClickGridConfigDto.ColDto colDto = colArr.get(i);
			
			if (i == 0) {
				sb.append(colDto.toString());
			} else {
				sb.append("," + colDto.toString());
			}
		}
		sb.append("]]");
		return sb.toString();
	}
}
